package com.monika.main.system.factory;

import com.monika.main.system.constant.Constant;
import com.monika.main.system.log.entity.OperateLog;
import com.monika.main.system.log.entity.SysUserLoginLog;
import com.monika.main.system.servcie.IOperLogService;
import com.monika.main.system.servcie.ISysUserLoginLogService;
import com.monika.main.system.util.*;
import eu.bitwalker.useragentutils.UserAgent;

import java.util.TimerTask;

/**
 * Author whh
 * Date 2023/04/15/ 22:55
 * <p></p>
 */
public class AsyncFactory {



    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status 状态
     * @param message 消息
     * @param args 列表
     * @return 任务task
     */
    public static TimerTask recordSysUserLoginLog(final String username, final String status, final String message,
                                             final Object... args)
    {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtil.getRequest().getHeader("User-Agent"));
        final String ip = IpUtil.getIpAddr();
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String address = AddressUtil.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(StringUtil.getBlock(ip));
                s.append(address);
                s.append(StringUtil.getBlock(username));
                s.append(StringUtil.getBlock(status));
                s.append(StringUtil.getBlock(message));
                // 打印信息到日志
                LogManager.LOG().sys_user_log(null).info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysUserLoginLog logininfor = new SysUserLoginLog();
                logininfor.setUserName(username);
                logininfor.setIpaddr(ip);
                logininfor.setLoginLocation(address);
                logininfor.setBrowser(browser);
                logininfor.setOs(os);
                logininfor.setMsg(message);
                // 日志状态
                if (StringUtil.equalsAny(status, Constant.LOGIN_SUCCESS, Constant.LOGOUT, Constant.REGISTER))
                {
                    logininfor.setStatus(Constant.SUCCESS);
                }
                else if (Constant.LOGIN_FAIL.equals(status))
                {
                    logininfor.setStatus(Constant.FAIL);
                }
                // 插入数据
                SpringUtil.getBean(ISysUserLoginLogService.class).insertSysUserLoginLog(logininfor);
            }
        };
    }

    /**
     * 操作日志记录
     *
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final OperateLog operLog)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                // 远程查询操作地点
                operLog.setOperLocation(AddressUtil.getRealAddressByIP(operLog.getOperIp()));
                SpringUtil.getBean(IOperLogService.class).insertOperateLog(operLog);
            }
        };
    }
}
